package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Calendar;

public class ExceptionLogger {
	private PrintWriter errorWriter = null;
	private File log;
	
    public void logException(Exception e) {
    	/*
    	 *  It always creates a new file, overwriting the old one.
    	 *  If it is activated while running the application from the IDE,
    	 *  you might have to Refresh your project to see the file, as it
    	 *  will appear in the root of your project.
    	 *  Since the IDE usually spits out all out need, this is mostly
    	 *  useful when running the application from a Jar-file.
    	 */
    	log = new File("ExceptionLog.txt");
		try {
			errorWriter = new PrintWriter(log);
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
		
    	if(errorWriter!=null){
    		String dateAndTime = ""+Calendar.getInstance().getTime().toString();
    		
    		errorWriter.println(dateAndTime);
    		
    		errorWriter.println("-----------------------------------------------------");
        	
        	e.printStackTrace(errorWriter);
        	
        	errorWriter.close();
        }
    	
    	e.printStackTrace();

        // Lidt "harsh", men vi siger bare at alle exceptions lukker programmet.
		System.exit(0);
    }
}